<?php
namespace app\web\controller;

use app\model\Util; 
use app\model\Theme; 
use app\model\Otherform; 

class FansOtherInfo extends Base
{
    public function Fans_other_info()
    {
/**
 * Author: cbw
 * Desc: 后台客资收集
 * Time: 2022/4/14 13:59
 * Email: 17854288794@163.com
 */
global $_P, $_G;
$page_index = max(1,intval($_P['page']));
$page_size = 50;
$page_start = ($page_index-1)*$page_size;

$themeid = intval($_P['themeid']);
if($themeid <= 0){
    $themeid = Theme::getDefaultThemenew();
}
$config =   Theme::getConfig($themeid);
$themelist = Theme::getAllTheme();
$status = $_P['status'];
$do = 'fans_other_info';
$op = $_P['op']?$_P['op']:'display';
$system = $_G['system'];
$method = 'otherform';
$pay_status = $_P['pay_status']?(int)$_P['pay_status']:0;
$fromuid = isset($_P['fromuid'])?(int)$_P['fromuid']:0;
$click_open_box = $_P['click_open_box']?$_P['click_open_box']:0;
$config = Theme::getConfig($themeid);



$is_special_mode = '';//
$show_help_flag = 0;
$show_help_flag_txt = '给谁助力';
//盲盒处理
if ($config['get_mystery_boxes']>0){
    $is_special_mode = '拆盲盒';
    $show_help_flag = 1;
}
//点亮星星处理
if ($config['isicon']==3 && $config['friendorder']==1){
    if ($config['iconziliao']!=1){
        //收集客资
        $show_help_flag = 1;
    }
    if ($config['friendorder']==1){
        //已报名、未报名客户都可邀请点亮
        $is_special_mode='报名助力';
    }
}

if ($op=='display'){
    $pay_status_arr = Otherform::getOrderStatusArr($themeid,$config,$fromuid);
    $status_arr = Otherform::getFlagTypeCount($themeid);
    $type_arr = [];
    if ($is_special_mode){
        $type_arr = Otherform::getFlagClickOpenBox($themeid,$config,$fromuid);
    }
    if (empty($_P['createtime']['start'])){
        $starttime =db_fetchcolumn ( 'SELECT min(addtime) FROM ' . tablename ( t_fans ) );
        $start_day =  date('Y-m-d H:i:s',$starttime);
    }else{
        $start_day =  $_P['createtime']['start'];
        $starttime = strtotime($start_day);
    }
    $end_day = empty($_P['createtime']['end']) ? date('Y-m-d H:i:s',TIMESTAMP): $_P['createtime']['end'] ;
    $endtime = empty($_P['createtime']['end']) ? TIMESTAMP: strtotime($_P['createtime']['end']);

    $search_data = isset($_P['search_data'])?$_P['search_data']:'';
    if ($search_data){
        $search_data = '%'.$search_data.'%';
    }
    if ($status){
        $search_data = '%'.$status.'%';
    }
    $format_fileds_arr = [];
    $format_fileds_arr = Otherform::formatConfig($themeid,2);
    if ($config['form_mode']>0 && $config['formtourl']>0){
        $from_themeid = $config['formtourl'];
    }else{
        $from_themeid = $themeid;
    }
    $list = Otherform::newSearchFlag($config,$themeid,$from_themeid,$search_data,$page_start,$page_size,$start_day,$end_day,$pay_status,false,0,$show_help_flag,$click_open_box);
    $total = Otherform::newSearchFlag($config,$themeid,$from_themeid,$search_data,$page_start,$page_size,$start_day,$end_day,$pay_status,true,0,$show_help_flag,$click_open_box);
    if(checksubmit('export_submit', true) || checksubmit('export_submit1', true) || checksubmit('export_submit2', true)){
        $count = $total;
        $ex_page = max(intval($_P['ex_page']),1);
        $form_filed_name = '上级';
        $page_limit = 2000;
        $page_start =($ex_page - 1) * $page_limit;
        $pagesize = ceil($count/$page_limit);
        if($system['softmenu']==1  && $system['ruanjian']==6){
            $form_filed_name = '被助力者';
        }
        $header = array(
            'aid' => '序号',
            'nickname' => '微信昵称',
        );
//        if ($show_help_flag==1){
            foreach ($format_fileds_arr as $kk => $vv){
                $header[$vv['key']]=$vv['name'];
            }
//        }

        $header['pay_status']='是否付款';
        if ($system['ispresale']==1 && $config['yukuan'] > 0 && TIMESTAMP > $config['yukuantime']) {
            $header['bukuan'] = '补款状态';
        }
        if ($is_special_mode){
            $header['click_open_box']=$is_special_mode;
        }
        if ($show_help_flag){
            $header['friend_list']=$show_help_flag_txt;
        }
        $header['created_at']='提交时间';


        $header['workername']='员工';
        $keys = array_keys($header);
        $html = "\xEF\xBB\xBF";
        foreach ($header as $li) {
            $html .= $li . "\t ,";
        }
        $html .= "\n";
        $export_type = 0;//导出模式  0 客资相关  1当前人的助力信息  2所有人的助力信息
        if (checksubmit('export_submit1', true)){
            $export_type = 1;
        }
        if (checksubmit('export_submit2', true)){
            $export_type = 2;
        }
        $list = Otherform::newSearchFlag($config,$themeid,$from_themeid,$search_data,$page_start,$page_limit,$start_day,$end_day,$pay_status,false,0,$show_help_flag,$click_open_box,true,$export_type);

        foreach($list as $k => $val){
            $temp_fileds = json_decode($val['fileds'],true);
            if ($temp_fileds && is_array($temp_fileds)){
                foreach ($temp_fileds as $kk => $vv){
                    $list[$k][$kk] = $vv;
                }
                unset($list[$k]['fileds']);
            }
            $list[$k]['bukuan'] = $val['pay_status']['bukuan'];
            $list[$k]['pay_status'] = $val['pay_status']['txt'];
        }


        if (!empty($list)) {
            $size = ceil(count($list) / 500);
            for ($i = 0; $i < $size; $i++) {
                $buffer = array_slice($list, $i * 500, 500);
                foreach ($buffer as $index => $row) {
                    $row['aid'] = $i*500 + $index+1;
                    if ($row['workername']){
                        $row['workername']=$row['companyname'].'-'.$row['teamname'].'-'.$row['workername'];
                    }
                    if ($show_help_flag==1){
                        $row['addtime'] = date("Y-m-d H:i:s",$row['addtime']);
                    }

                    if ($row['nickname']){
                        $row['nickname'] = trim($row['nickname'],",");
                        $row['nickname'] = trim($row['nickname'],'"');
                        $row['nickname'] = trim($row['nickname'],"'");
                    }
                    if ($show_help_flag==1){
                        $row['click_open_box'] = $row['click_open_box']?'是':'否';
                        $tmp_friend_list = $row['friend_list'];
                        $tmp_friend_list_str = '';
                        if (!empty($tmp_friend_list)){
                            foreach ($tmp_friend_list as $val){
                                if (!empty($val['username'])){
                                    $tmp_friend_list_str .= $val['username'].'-'.$val['mobile'].';';
                                }
                            }
                        }
                        $row['friend_list'] = $tmp_friend_list_str;
                    }
                    foreach ($keys as $key) {
                        $data[] = $row[$key];
                    }
                    if (isset($data)){
                        $user[] = implode("\t ,", $data) . "\t ,";
                        unset($data);
                    }
                }
                $html .= implode("\n", $user)."\n";
                unset($user);
            }
        }
//        }
        //新版本导出合并
        $is_end = 0;
        if($ex_page>=$pagesize){
            $is_end = 1;
        }
        Util::WxJump($system,$html,'表单收集信息',$themeid,$is_end,$pagesize);
    }
    $pager = pagination($total,$page_index,$page_size);
    $return_clicent = get_defined_vars();
return view('/fans_other_info' , $return_clicent);
}

if($op == 'delete'){
    if ($config['isicon']==3 || $config['get_mystery_boxes']>0){
        $item = db_get(t_friend,['id'=>$_P['id']]);
    }else{
        $item = Otherform::getItem(['id'=>$_P['id']]);
    }

    if (empty($item)){
        return_msg('表单信息不存在');
    }
    yxlog($item,'表单信息删除');
    if ($config['isicon']==3 || $config['get_mystery_boxes']>0){
        //根据fromuid 查询fans中的助力数
        $from_fans = db_get(t_fans,['themeid'=>$themeid,'uid'=>$item['fromuid']]);
        if ($from_fans && $from_fans['friendnum']>1){
            $update_fans_sql = " update ".tablename(t_fans)." set friendnum=friendnum-1 where uid='".$from_fans['uid']."' and themeid='".$themeid."'";
            db_find($update_fans_sql);
        }
        db_delete ( t_friend, array ('id' => $_P['id']) );
    }else{
        db_delete ( t_fans_other_form, array ('id' => $_P['id']) );
    }
    $url = createWebUrl($do,array('op' => 'display','themeid'=>$themeid));
    if (isset($_P['fromuid']) && $_P['fromuid']){
        $url = createWebUrl($do,array('op' => 'display','themeid'=>$themeid,'fromuid'=>$_P['fromuid']));
    }

    return_tips('删除成功！', $url, 'success');
}
}
}